package com.whf.service.impl;

import com.whf.dataobject.ProductInfo;
import com.whf.dto.CartDTO;
import com.whf.enums.ProductStatusEnum;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import com.whf.repository.ProductInfoRepository;
import com.whf.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * Created by Administrator on 2018/7/12 0012.
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductInfoRepository repository;
    @Override
    public ProductInfo findOneByProductId(String productId) {
        return repository.findById(productId).get();
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO:cartDTOList){
            //根据商品id查询商品
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //获取商品库存与销售数量相加
            Integer result = productInfo.getProductStock() +cartDTO.getProductQuantity();

            //设置库存
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }

    }

    @Override
    public void decreaseStock(List<CartDTO> cartDTOList) {

        for(CartDTO cartDTO:cartDTOList){
            //根据id查询商品
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //判断商品库存
            Integer result=productInfo.getProductStock()-cartDTO.getProductQuantity();
            if(result<0){
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            repository.save(productInfo);

        }
    }
//下架商品
    @Override
    public ProductInfo downSave(String productId) {
        ProductInfo productInfo = repository.findById(productId).get();
        if(productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatus()==1){
            throw new SellException(ResultEnum.ERROR_PRODUCT_STATUS);
        }
        productInfo.setProductStatus(1);
        ProductInfo save = repository.save(productInfo);
        return save;
    }
//上架商品
    @Override
    public ProductInfo upSave(String productId) {
        ProductInfo productInfo = repository.findById(productId).get();
        if(productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatus()==0){
            throw new SellException(ResultEnum.ERROR_PRODUCT_STATUS);
        }
        productInfo.setProductStatus(0);
        ProductInfo save = repository.save(productInfo);
        return save;
    }
}
